package com.ltxtiyu.video.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.ltxtiyu.video.entity.Topic;
import com.ltxtiyu.video.service.ms.ConfigService;
import com.ltxtiyu.video.service.ms.TopicService;
import com.ltxtiyu.video.tool.StringUtil;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import sun.misc.Request;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

@Controller
@RequestMapping("topicController")
public class TopicController {

    @Reference
    private TopicService ts;
    @Reference
    private ConfigService configService;

    /**
     * 条件查询列表
     * @param page
     * @param limit
     * @param name
     * @param isHotPush
     * @param isHotSearch
     * @return
     */
    @RequestMapping(value = "paramlist",method = RequestMethod.GET)
    public ResponseEntity<Map<String ,Object>> queryById(int page,int limit, String name,Integer  isHotPush,Integer isHotSearch){
        try {
            Map<String,Object> map = new HashMap<>();
            map.put("page",(page-1)*limit);
            map.put("row",limit);
            if(StringUtil.isNotBlank(name)){

                map.put("name",name);
            }
            map.put("isHotPush",isHotPush);
            map.put("isHotSearch",isHotSearch);
            map.put("remindType",100);
            Map<String,Object> result = ts.selectParamList(map);

            return ResponseEntity.ok(result);
        }catch (Exception e){
            e.printStackTrace();
            return  ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(null);
        }

    }

    //更新
    @RequestMapping(value = "update",method = RequestMethod.PUT)
    public  ResponseEntity<Void> update(Topic topic){
        try {
            if(topic == null){
                return ResponseEntity.status(HttpStatus.BAD_REQUEST).build();
            }
            ts.updateTopic(topic);
            return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
        }catch (Exception e){
            e.printStackTrace();
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();

    }

    /**
     * 删除
     * @param id
     * @return
     */
    @RequestMapping(value = "delById")
    public ResponseEntity<Void> delById(Integer id){
        try {
            if (id.longValue() <1){
                return ResponseEntity.status(HttpStatus.BAD_REQUEST).build();
            }
            ts.delByID(id);

            return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
        }catch (Exception e){
            e.printStackTrace();
        }

        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();

    }

    /**
     * topic查看地址
     * @param request
     * @param id
     * @return
     */
    @RequestMapping(value = "getUrl",method = RequestMethod.GET)
    public ResponseEntity<String> topicUrl(HttpServletRequest request,Integer id){
        try {
            String shareServerUrl=configService.selectByCkey("WX_DOMAIN").getCvalue();
            String url = shareServerUrl+"/topic?topicId="+id;
            return ResponseEntity.ok(url);
        }catch (Exception e){
            e.printStackTrace();
            return  ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(null);
        }
    }

}
